package com.song.web;

import com.song.bean.Menu;
import com.song.service.impl.MenuServiceImpl;
import com.song.util.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/power/menu/menumanager")
public class MenuServlet extends HttpServlet {
    private  MenuServiceImpl menuService = new MenuServiceImpl();
    /**
     * 根据method的值，判断下一步操作
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("select".equals(method)){
            select(req, resp);
        }else if ("findById".equals(method)){
            findById(req, resp);
        }else if ("update".equals(method)){
            update(req, resp);
        }else if ("insert".equals(method)){
            insert(req, resp);
        }else if ("insert_select_menu".equals(method)){
            insert_select_menu(req, resp);
        }else if ("delete".equals(method)){
            delete(req, resp);
        }
    }

    /**
     * 删除菜单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void delete(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException{
        String menuId = req.getParameter("menuId");
        //调用service
        Boolean aBoolean = menuService.deleteMenu(Integer.parseInt(menuId));
        if (aBoolean){
            resp.getWriter().println("<script>alert('删除成功');location.href='/power/menu/menumanager?method=select'</script>");
        }else{
            resp.getWriter().println("<script>alert('添加失败');location.href='/power/menu/menumanager?method=select'</script>");

        }

    }

    /**
     * 设置动态父级菜单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void insert_select_menu(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException{
        //调用service
        List<Menu> menus = menuService.getMenus();
        req.setAttribute("menus",menus);
        req.getRequestDispatcher("add.jsp").forward(req,resp);

    }

    /**
     * 新增菜单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void insert(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException{
        //获取前端修改请求信息
        String menuName = req.getParameter("menuName");
        String fatherMenuId = req.getParameter("fatherMenuId");
        String url = req.getParameter("url");
        String menuState = req.getParameter("menuState");
        String remark = req.getParameter("remark");
        //String menuId = req.getParameter("menuId");
        Menu menu = new Menu();
        menu.setMenuName(menuName);
        menu.setFatherMenuId(Integer.parseInt(fatherMenuId));
        menu.setUrl(url);
        menu.setMenuState(Integer.parseInt(menuState));
        menu.setRemark(remark);
        //menu.setMenuId(Integer.parseInt(menuId));
        //调用service
        Boolean aBoolean = menuService.insertMenu(menu);
        if (aBoolean){
            resp.getWriter().println("<script>alert('添加成功');location.href='/power/menu/menumanager?method=select'</script>");
        }else {
            resp.getWriter().println("<script>alert('添加失败');location.href='/power/menu/menumanager?method=select'</script>");
        }
    }

    /**
     * 修改菜单信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void update(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException{
            //获取前端修改请求信息
        String menuName = req.getParameter("menuName");
        String fatherMenuId = req.getParameter("fatherMenuId");
        String url = req.getParameter("url");
        String menuState = req.getParameter("menuState");
        String remark = req.getParameter("remark");
        String menuId = req.getParameter("menuId");
        Menu menu = new Menu();
        menu.setMenuName(menuName);
        menu.setFatherMenuId(Integer.parseInt(fatherMenuId));
        menu.setUrl(url);
        menu.setMenuState(Integer.parseInt(menuState));
        menu.setRemark(remark);
        menu.setMenuId(Integer.parseInt(menuId));
        //调用service
        Boolean aBoolean = menuService.updateMenu(menu);
        if (aBoolean) {
            resp.getWriter().println("<script>alert('修改成功');location.href='/power/menu/menumanager?method=select'</script>");
        }else{
            resp.getWriter().println("<script>alert('修改失败');location.href='/power/menu/menumanager?method=select'</script>");
        }
    }

    /**
     * 通过菜单ID查询菜单信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获取前端请求信息
        String menuId = req.getParameter("menuId");
        //调用service
        Menu menu = menuService.findById(Integer.parseInt(menuId));
        //获取菜单分级
        List<Menu> menus = menuService.getMenus();
        req.setAttribute("menus",menus);
        req.setAttribute("menu",menu);
        System.out.println(menu.getFatherMenuId());
        req.getRequestDispatcher("edit.jsp").forward(req, resp);
    }

    /**
     * 查询菜单信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获取前端信息
        String index = req.getParameter("index");
        int pageIndex = (index == null||index.length() == 0) ? 1 : Integer.parseInt(index);
        //调用分页工具
        PageUtils pageUtils = new PageUtils();
        //调用service
        List<Menu> menuList = menuService.getMenus(pageIndex,pageUtils.getPagesize());
        int total = menuService.getMenus(null, null).size();
        System.out.println(menuList.size());
        pageUtils.setIndex(pageIndex);
        pageUtils.setTotal(total);
        if (menuList != null){
            req.setAttribute("menus",menuList);
            req.setAttribute("page",pageUtils);
            req.getRequestDispatcher("list.jsp").forward(req, resp);
        }else{
            resp.getWriter().println("<script>alert('没有信息');location.href='/power/menu/list.jsp'</script>");
        }
    }
}
